﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessPlatform.Core.BusinessObject;
using BusinessPlatform.Data;
using System.Reflection;
using BusinessPlatform.Core.BusinessObject.Properties;
using BusinessPlatform.Core.BusinessObject.BOParser;
using BusinessPlatform.Core.Database;

namespace BusinessPlatform.Core.BusinesObjectType
{
    /// <summary>
    /// 对象处理
    /// </summary>
    internal class ObjectHandling : IObjectHandling
    {
        private IBusinessObjectBase EntityObject { get; set; }
        private EnumBusinessObjectType ObjectType { get; set; }
        public ObjectHandling(IBusinessObjectBase entity)
        {
            EntityObject = entity;
            #region 获取对象的业务类型
            foreach (var interfacename in EntityObject.GetType().GetInterfaces())
            {
                if (interfacename.IsInterface)
                    switch (interfacename.Name)
                    {
                        case "IDocument":
                            ObjectType = EnumBusinessObjectType.Document;
                            break;
                        case "IDocumentLine":
                            ObjectType = EnumBusinessObjectType.DocumentLine;
                            break;
                        case "IMasterData":
                            ObjectType = EnumBusinessObjectType.MasterData;
                            break;
                        case "IMasterDataLine":
                            ObjectType = EnumBusinessObjectType.MasterDataLine;
                            break;
                        case "ISimple":
                            ObjectType = EnumBusinessObjectType.Simple;
                            break;
                    }
            }
            #endregion
        }
        /// <summary>
        /// 设置数据对象的属性值
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityObject"></param>
        private void SetDataEntityPropertyValue(IDatabase db, IBusinessObjectBase EntityObject)
        {
            var DataEntity = (EntityObject as IDataStructure);
            var PropertyInfo = default(IPropertyInfo);
            //对象类型

            //创建人
            PropertyInfo = (EntityObject as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Creator");
            if (PropertyInfo.PropertyValue == null)
                DataEntity.Creator = -1;
            //创建时间
            DataEntity.CreateDate = DateTime.Now;
            #region 处理子对象
            IBOParser boParser = new BOParser(EntityObject);
            foreach (var item in boParser.Children)
            {
                foreach (var line in item.Children)
                {
                    var DataEntityLine = (line as IDataStructure);
                    //对象类型
                    DataEntityLine.ObjectType = DataEntity.ObjectType;
                    //创建人
                    PropertyInfo = (line as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Creator");
                    if (PropertyInfo.PropertyValue == null)
                        DataEntityLine.Creator = -1;
                    //创建时间
                    DataEntityLine.CreateDate = DateTime.Now;
                }
            }
            #endregion
        }
        /// <summary>
        /// 设置单据对象的属性值
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityObject"></param>
        private void SetDocumentPropertyValue(IDatabase db, IBusinessObjectBase EntityObject)
        {
            //设置数据对象的属性值
            SetDataEntityPropertyValue(db, EntityObject);
            var EntityDocument = (EntityObject as IDocument);
            //获取对象的单据编号，唯一值
            EntityDocument.DocumentEntry = GetObjectValue.GetUniqueKey(db, EntityDocument);
            //如果此字段为空则使用单据编码，唯一值
            if (string.IsNullOrWhiteSpace(EntityDocument.DocumentNumber))
                EntityDocument.DocumentNumber = EntityDocument.DocumentEntry.ToString();
            var PropertyInfo = default(IPropertyInfo);
            //取消状态
            PropertyInfo = (EntityObject as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Canceled");
            if (PropertyInfo.PropertyValue == null)
                EntityDocument.Canceled = Enumerator.BOYesOrNo.No;
            //关闭状态
            PropertyInfo = (EntityObject as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Closed");
            if (PropertyInfo.PropertyValue == null)
                EntityDocument.Closed = Enumerator.BOYesOrNo.No;
            //单据状态
            PropertyInfo = (EntityObject as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("DocumentStatus");
            if (PropertyInfo.PropertyValue == null)
                EntityDocument.DocumentStatus = Enumerator.BODocStatus.Open;
            #region 处理子对象
            IBOParser boParser = new BOParser(EntityObject);
            foreach (var item in boParser.Children)
            {
                int RowNumber = 0;
                foreach (var line in item.Children)
                {
                    RowNumber++;
                    var EntityDocumentLine = (line as IDocumentLine);
                    //单据编号
                    EntityDocumentLine.DocumentEntry = EntityDocument.DocumentEntry;
                    //行号
                    EntityDocumentLine.LineNumber = RowNumber;
                    EntityDocumentLine.VisilbeOrder = RowNumber;
                    //取消状态
                    PropertyInfo = (line as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Canceled");
                    if (PropertyInfo.PropertyValue == null)
                        EntityDocumentLine.Canceled = Enumerator.BOYesOrNo.No;
                    //关闭状态
                    PropertyInfo = (line as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Closed");
                    if (PropertyInfo.PropertyValue == null)
                        EntityDocumentLine.Closed = Enumerator.BOYesOrNo.No;
                    //单据状态
                    PropertyInfo = (line as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("LineStatus");
                    if (PropertyInfo.PropertyValue == null)
                        EntityDocumentLine.LineStatus = Enumerator.BOLineStatus.Open;
                }
            }
            #endregion
        }
        /// <summary>
        /// 设置主数据对象的属性值
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityObject"></param>
        private void SetMasterDataPropertyValue(IDatabase db, IBusinessObjectBase EntityObject)
        {
            SetDataEntityPropertyValue(db, EntityObject);
            var EntityMasterData = (EntityObject as IMasterData);
            var PropertyInfo = default(IPropertyInfo);
            //获取单据值
            PropertyInfo = (EntityObject as IBusinessObjectProperties).PropertiesManager.GetPrimaryPropertyInfo();
            PropertyInfo.PropertyValue = GetObjectValue.GetUniqueKey(db, EntityMasterData);
            //活动
            PropertyInfo = (EntityObject as IBusinessObjectProperties).PropertiesManager.GetPropertyInfoByPropertyName("Activated");
            if (PropertyInfo.PropertyValue == null)
                EntityMasterData.Activated = Enumerator.BOYesOrNo.Yes;
            IBOParser boParser = new BOParser(EntityObject);
            foreach (var item in boParser.Children)
            {
                int RowNumber = 0;
                foreach (var line in item.Children)
                {
                    RowNumber++;
                    var EntityMasterLine = (line as IMasterDataLine);
                    //行号
                    EntityMasterLine.LineNumber = RowNumber;
                    EntityMasterLine.VisilbeOrder = RowNumber;                    
                }
            }
        }
        /// <summary>
        /// 对象添加
        /// </summary>
        /// <param name="db"></param>
        public void New(IDatabase db)
        {
            switch (ObjectType)
            {
                case EnumBusinessObjectType.Document:
                    SetDocumentPropertyValue(db, EntityObject);
                    break;
                case EnumBusinessObjectType.MasterData:
                    SetMasterDataPropertyValue(db, EntityObject);
                    break;
            }
        }

        public void Update()
        {

        }
    }
}
